/**
 * 本地图片压缩
 */

const composerWidthHeight = (image, fileSize) => {
  let targetWidth = image.width;
  let targetHeight = image.height;
  let originWidth = image.width;
  let originHeight = image.height;

  if (fileSize > 10 * 1024) {
    const maxWidth = 1400;
    const maxHeight = 1400;
    targetWidth = originWidth;
    targetHeight = originHeight;
    if (originWidth > maxWidth || originHeight > maxHeight) {
      if (originWidth / originHeight > maxWidth / maxHeight) {
        targetWidth = maxWidth;
        targetHeight = Math.round(maxWidth * (originHeight / originWidth));
      } else {
        targetHeight = maxHeight;
        targetWidth = Math.round(maxHeight * (originWidth / originHeight));
      }
    }
  } else {
    const maxWidth = 1600;
    const maxHeight = 1600;
    targetWidth = originWidth;
    targetHeight = originHeight;
    if (originWidth > maxWidth || originHeight > maxHeight) {
      if (originWidth / originHeight > maxWidth / maxHeight) {
        targetWidth = maxWidth;
        targetHeight = Math.round(maxWidth * (originHeight / originWidth));
      } else {
        targetHeight = maxHeight;
        targetWidth = Math.round(maxHeight * (originWidth / originHeight));
      }
    }
  }

  return { targetWidth, targetHeight };
}

export const compressImg = (file, quality = null) => {
  const fileSize = parseInt((file.size / 1024).toFixed(2));
  let qualitys = 0.52;
  if (fileSize < 1024) {
    qualitys = 0.9;
  }
  if (fileSize > 5 * 1024) {
    qualitys = 0.7;
  }
  if (fileSize > 10 * 1024) {
    qualitys = 0.5;
  }
  if (quality) {
    qualitys = quality
  }
  if (file[0]) {
    return Promise.all(Array.from(file).map(e => compressImg(e,
      qualitys)));
  } else {
    return new Promise((resolve) => {
      const reader = new FileReader(); // 创建 FileReader
      reader.readAsDataURL(file);
      reader.onload = (event) => {
        const { target: { result: src } } = event;
        const image = new Image(); // 创建 img 元素
        image.src = src;
        image.onload = async () => {
          const canvas = document.createElement('canvas'); // 创建 canvas 元素
          const context = canvas.getContext('2d');
          const { targetWidth, targetHeight } = composerWidthHeight(image, fileSize);

          canvas.width = targetWidth;
          canvas.height = targetHeight;
          context.clearRect(0, 0, targetWidth, targetHeight);
          context.drawImage(image, 0, 0, targetWidth, targetHeight);
          const canvasURL = canvas.toDataURL('image/jpeg', qualitys);
          const buffer = atob(canvasURL.split(',')[1]);
          let length = buffer.length;
          const bufferArray = new Uint8Array(new ArrayBuffer(length));
          while (length--) {
            bufferArray[length] = buffer.charCodeAt(length)
          }
          const miniFile = new File([bufferArray], file.name, { type: 'image/jpeg' });

          resolve({
            file: miniFile,
            origin: file,
            beforeSrc: src,
            afterSrc: canvasURL,
            beforeKB: fileSize,
            afterKB: Number((miniFile.size / 1024).toFixed(2))
          });
        }
      }
    });
  }
}
window.compressImg = compressImg;
export default compressImg;
